package cn.huijielong.modules.pub.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.huijielong.common.utils.Query;
import cn.huijielong.modules.pub.dao.MerchantDao;
import cn.huijielong.modules.pub.entity.MerchantEntity;
import cn.huijielong.modules.pub.service.MerchantService;
import cn.huijielong.modules.sys.service.impl.SimpleServiceImpl;



@Service("merchantService")
public class MerchantServiceImpl extends SimpleServiceImpl implements MerchantService {
	@Autowired
	private MerchantDao merchantDao;
	
	@Override
	public MerchantEntity queryObject(Long merchantId){
		return merchantDao.queryObject(merchantId);
	}
	
	@Override
	public Page<MerchantEntity> queryList(Query q){
	    Page<MerchantEntity> page=PageHelper.startPage(q.getPage(), q.getLimit()).doSelectPage(()-> merchantDao.queryList(q));
		return page;		
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return merchantDao.queryTotal(map);
	}
	
	@Override
	public void save(MerchantEntity merchant){
		merchantDao.save(merchant);
	}
	
	@Override
	public void update(MerchantEntity merchant){
		merchantDao.update(merchant);
	}
	
	@Override
	public void delete(Long merchantId){
		merchantDao.delete(merchantId);
	}
	
	@Override
	public void deleteBatch(Long[] merchantIds){
		merchantDao.deleteBatch(merchantIds);
	}

	@Override
	public Long queryMerchantId(Long userId) {
		return merchantDao.queryMerchantId(userId);
	}
	@Override
	public List<Long> queryMerchants(Long userId) {
		return merchantDao.queryMerchants(userId);
	}
	@Override
	public Long querySelectedMerchantId(Long userId) {
		Long merchantId= merchantDao.querySelectedMerchantId(userId);
		if(merchantId==null)
			return Long.valueOf(0);
		return merchantId;
	}

	@Override
	public List<Query> queryUserByMerId(Long merchantId) {
		return merchantDao.queryUserByMerId(merchantId);
	}

	@Override
	public int saveMerchantUser(Query q) {
		Long userId=q.getLong("userId");
		Long merId=merchantDao.querySelectedMerchantId(userId);
		if(merId==null)
			q.addParam("selected", "0");
		else
			q.addParam("selected", "1");
		return merchantDao.saveMerchantUser(q);
	}

	@Override
	public void deleteUser(Query data) {
		merchantDao.deleteUser(data);
	}
	
}
